class Solution {
    public int tribonacci(int n) {
        int[] dp = new int[n + 1];
        if(n == 0) { return 0;}
        if(n == 1 || n == 2) { return 1;}

        dp[0] = 0;
        dp[1] = dp[2] = 1;
        for(int i = 3; i <= n; i++){
            dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
        }
        return dp[n];
    }

    public int Newtribonacci(int n) {
        if(n == 0) { return 0;}
        if(n == 1 || n == 2) { return 1;}

        int a = 0, b = 1, c = 1, d = 0;
        for(int i = 3; i <= n; i++){
            d = a + b + c;
            //滚动操作
            a = b;
            b = c;
            c = d;
        }
        return d;
    }

    public int waysToStep(int n) {
        if(n == 1 || n == 2) { return n;}
        if(n == 3) { return 4;}

        int MOD = (int)1e9 + 7;
        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 4;

        for(int i = 4; i <= n; i++){
            dp[i] = (dp[i - 1] + (dp[i - 2] + dp[i - 3]) % MOD) % MOD;
        }
        return dp[n];
    }

    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n + 1];

        for(int i = 2; i <= n; i++){
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        return dp[n];
    }

    public int numDecodings(String s) {
        int n = s.length();
        char[] ch = s.toCharArray();

        int[] dp = new int[n + 1];
        dp[0] = 1;
        if(ch[0] != '0') { dp[1] = 1;}

        for(int i = 2; i <= n; i++){
            if(ch[i - 1] != '0') { dp[i] = dp[i - 1];}
            int tmp = (ch[i - 2] - '0') * 10 + (ch[i - 1] - '0');
            if(tmp >= 10 && tmp <= 26) { dp[i] += dp[i - 2];}
        }
        return dp[n];
    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        int[][] dp = new int[m + 1][n + 1];
        dp[0][1] = 1;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(obstacleGrid[i - 1][j - 1] == 0){
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
        }
        return dp[m][n];
    }

    public int minFallingPathSum(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        int[][] dp = new int[m + 1][n + 2];

        for(int j = 0; j <= n; j++) { dp[0][j] = 0;}
        for(int i = 1; i <= m; i++) {
            dp[i][0] = Integer.MAX_VALUE;
            dp[i][n + 1] = Integer.MAX_VALUE;
        }

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i - 1][j + 1])) + matrix[i - 1][j - 1];
            }
        }

        int ret = Integer.MAX_VALUE;
        for(int j = 1; j <= n; j++){
            ret = Math.min(ret, dp[m][j]);
        }
        return ret;
    }

    public int minPathSum(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[][] dp = new int[m + 1][n + 1];
        for(int j = 0; j <= n; j++) { dp[0][j] = Integer.MAX_VALUE;}
        for(int i = 0; i <= m; i++) { dp[i][0] = Integer.MAX_VALUE;}
        dp[0][1] = 0;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];
            }
        }
        return dp[m][n];
    }

    public int calculateMinimumHP(int[][] dungeon) {
        int m = dungeon.length, n = dungeon[0].length;
        int[][] dp = new int[m + 1][n + 1];
        for(int j = 0; j <= n; j++) { dp[m][j] = Integer.MAX_VALUE;}
        for(int i = 0; i <= m; i++) { dp[i][n] = Integer.MAX_VALUE;}
        dp[m - 1][n] = 1;

        for(int i = m - 1; i >= 0; i--){
            for(int j = n - 1; j >= 0; j--){
                dp[i][j] = Math.min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j];
                dp[i][j] = Math.max(dp[i][j], 1);
            }
        }
        return dp[0][0];
    }

    public int massage(int[] nums) {
        int n = nums.length;
        int[] f = new int[n + 1];
        int[] g = new int[n + 1];

        for (int i = 1; i <= n; i++) {
            f[i] = g[i - 1] + nums[i - 1];
            g[i] = Math.max(f[i - 1], g[i - 1]);
        }
        return Math.max(f[n], g[n]);
    }

    public int get(int[] nums, int left, int right){
        if(left > right) { return 0;}
        int[] f = new int[nums.length];
        int[] g = new int[nums.length];
        f[left] = nums[left];
        for(int i = left + 1; i <= right; i++){
            f[i] = g[i - 1] + nums[i];
            g[i] = Math.max(f[i - 1], g[i - 1]);
        }
        return Math.max(f[right], g[right]);
    }
    public int rob(int[] nums) {
        int n = nums.length;
        return Math.max(nums[0] + get(nums, 2, n - 2), get(nums, 1, n - 1));
    }
}